package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.loadmore.OnLoadMoreListener;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWhitTitleNoBackActivity;
import com.uchengwang.ui.cm_ui.cycle.adapter.CycleListAdapter;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.login.ui.SelectSchoolActivity;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/5/27.
 * 社圈列表页
 */

public class CycleListActivity extends BaseWhitTitleNoBackActivity{
    private TextView list_school;

    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private CycleListAdapter adapter;
    private int page =1, totlaPagers;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_cycle_list);
        getRy().setTitle("社圈列表");

        EventBus.getDefault().register(this);

        getRy().setRightText("创建", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (UChengApplication.myUniId <=0){
                    //说明没有选择学校 跳转到选择学校界面 选择后直接跳转到邀请码界面
                    Intent intent = new Intent(CycleListActivity.this, SelectSchoolActivity.class);
                    intent.putExtra(BaseConfig.TYPE, 2);
                    startActivity(intent);
                }else {
                    startActivity(new Intent(CycleListActivity.this, InviteCodeCreateCycleActivity.class));
                }
            }
        });


        
        initView();
    }

    private void initView(){
        list_school = (TextView) findViewById(R.id.list_school);
        recycleView = (RecyclerView) findViewById(R.id.list_rv);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.list_layout);

        initData();

    }

    private void initData(){

        adapter = new CycleListAdapter(this);
        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.setAutoLoadMoreEnable(true);
        mSwipeRefreshLayout.setLoadMoreEnable(true);

        mSwipeRefreshLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {
            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                mSwipeRefreshLayout.loadMoreComplete(true);
                getDatas(true);

            }
        });

        mSwipeRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {

            @Override
            public void loadMore() {
                page++;
                if (page <= totlaPagers){
                    //当前请求页大于总页数，不再请求
                    getDatas(false);
                } else {
                    mSwipeRefreshLayout.loadMoreComplete(false);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!TextUtils.isEmpty(UChengApplication.mySchoolName)){
            list_school.setText(UChengApplication.mySchoolName);
        }
    }

    //获取列表
    private void getDatas(final boolean isRefresh){
        CycleHttpUtils.getUtils().getCycles(this, 0, 0, page, 20, new OnHttpResultListener<CycleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleBean> list) {
                if (isRefresh){
                    mSwipeRefreshLayout.refreshComplete();
                }else {
                    mSwipeRefreshLayout.loadMoreComplete(true);
                }

                totlaPagers = total;


                if (list != null){
                    if (isRefresh){
                        adapter.setData(list);
                    }else {
                        adapter.getData().addAll(list);
                        adapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    /**
     * 收到EventBus信息
     * */
    public void onEvent(CycleBean cycleBean) {
        if (cycleBean != null){
            long cycleId = cycleBean.getId();
            List<CycleBean> list = adapter.getData();
            for (CycleBean bean: list){
                if (bean.getId() ==cycleId){
                    bean.setIsjoin(0);
                    adapter.notifyDataSetChanged();
                    break;
                }
            }

        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
